package com.hooper.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hooper.hosp.model.order.OrderInfo;
import com.hooper.hosp.vo.order.OrderQueryVo;
import com.hooper.hospital.common.result.Result;
import com.hooper.hospital.common.utils.AuthContextHolder;
import com.hooper.order.service.OrderService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Tim Hooper
 * @version 1.0
 * @time 2022/10/09/19:54
 */
@RestController
@RequestMapping("/api/order")
@RequiredArgsConstructor
@EnableTransactionManagement
public class OrderController {

    private final OrderService orderService;

    @ApiOperation(value = "创建订单")
    @PostMapping("auth/submitOrder/{scheduleId}/{patientId}")
    public Result submitOrder(
            @ApiParam(name = "scheduleId", value = "排班id", required = true)
            @PathVariable String scheduleId,
            @ApiParam(name = "patientId", value = "就诊人id", required = true)
            @PathVariable Long patientId) {
        return Result.ok(orderService.saveOrder(scheduleId, patientId));
    }

    @ApiOperation(value = "获取订单信息")
    @GetMapping("/{orderId}")
    public Result<OrderInfo> getOrderInfo(@PathVariable String orderId) {
        OrderInfo orderInfo = orderService.getOrderById(orderId);
        return Result.ok(orderInfo);
    }

    @ApiOperation("获取用户的所有订单记录 带条件")
    @GetMapping("/{currentPage}/{pageSize}")
    public Result<IPage<OrderInfo>> getOrderPage(@PathVariable Integer currentPage,
                                                 @PathVariable Integer pageSize,
                                                 OrderQueryVo queryVo,
                                                 HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        queryVo.setUserId(userId);
        IPage<OrderInfo> page = orderService.pageByConditions(currentPage, pageSize, queryVo,userId);
        return Result.ok(page);
    }

    @ApiOperation("取消预约")
    @PostMapping("/cancel/{orderId}")
    public Result<Void> cancelOrder(@PathVariable String orderId){
        boolean canceled = orderService.cancelOrder(orderId);
        return canceled ? Result.ok() : Result.fail();
    }
}
